/**
 *
 */
package com.sihai.ehr.web.action.ajax;

import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.Auditing;
import com.sihai.ehr.model.biz.DutyProxy;
import com.sihai.ehr.service.AuditService;
import com.sihai.ehr.service.DutyProxyService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.util.List;

/**
 * @author wxn
 * @since 2012-7-14 23:21
 */
public class AuditAction extends EHRAction {

    @BeanInject
    private AuditService auditService;
    
    private boolean result;

    private Auditing auditing;

    private String batchIds;//批量操作对象对应的auditingTargetId列表

    //添加审核配置
    @Action(value = "/ajax/audit-config-add", results = {
            @Result(name = "success", type = "json", params = {"root", "auditing"})
    })
    public String add() {
        if(auditing != null){
            auditing.setId(null);
            auditService.save(auditing);
        }
        return SUCCESS;
    }

    //批量添加审核配置
    @Action(value = "/ajax/audit-config-batch-add", results = {
            @Result(name = "success", type = "json", params = {"root", "result"})
    })
    public String batchAdd() {
        if(auditing != null){
            auditing.setId(null);
            auditService.save(auditing, batchIds);
            result = true;
        }
        return SUCCESS;
    }

    //修改审核配置
    @Action(value = "/ajax/audit-config-edit", results = {
            @Result(name = "success", type = "json", params = {"root", "result"})
    })
    public String edit() {
        if(auditing != null && auditing.getId() != null && auditing.getId() > 0){
            Auditing a = auditService.get(auditing.getId());
            if(a != null){
                a.setAuditingNo(auditing.getAuditingNo());
                a.setAuditingSerialNo(auditing.getAuditingSerialNo());
                a.setIndicator(auditing.getIndicator());
                a.setSeq(auditing.getSeq());
                a.setDuty(auditing.getDuty());
                a.setEmployee(auditing.getEmployee());
                a.setDuty(auditing.getDuty());
                auditService.save(a);
                result = true;
            }
        }
        return SUCCESS;
    }

    //删除审核配置
    @Action(value = "/ajax/audit-config-remove", results = {
            @Result(name = "success", type = "json", params = {"root", "result"})
    })
    public String remove() {
        if(auditing!= null && auditing.getId() != null && auditing.getId() > 0){
            auditService.remove(auditing.getId());
        }
        return SUCCESS;
    }

    //重置
    @Action(value = "/ajax/audit-config-reset", results = {
            @Result(name = "success", type = "json", params = {"root", "result"})
    })
    public String reset() {
        if(auditing!= null){
            try{
                auditService.removeAll(auditing);
                result = true;
            }catch (Exception e){
                //e.printStackTrace();
                result = false;
            }
        }
        return SUCCESS;
    }

    //批量重置
    @Action(value = "/ajax/audit-config-batch-reset", results = {
            @Result(name = "success", type = "json", params = {"root", "result"})
    })
    public String batchReset() {
        if(auditing!= null){
            try{
                if(StringUtils.isNotBlank(batchIds)){
                    auditService.removeAll(auditing, batchIds);//批量删除
                }
                result = true;
            }catch (Exception e){
                //e.printStackTrace();
                result = false;
            }
        }
        return SUCCESS;
    }

    //保存顺序
    @Action(value = "/ajax/audit-config-saveorders", results = {
            @Result(name = "success", type = "json", params = {"root", "result"})
    })
    public String saveOrders() {
        String temp = getParameter("orders");
        String[] orders = StringUtils.split(temp, ",");
        if(orders != null && orders.length > 0){
            try{
                auditService.saveOrders(orders);
                result = true;
            }catch (Exception e){
                //e.printStackTrace();
                result = false;
            }
        }
        return SUCCESS;
    }

    public boolean isResult() {
        return result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }

    public Auditing getAuditing() {
        return auditing;
    }

    public void setAuditing(Auditing auditing) {
        this.auditing = auditing;
    }

    public void setBatchIds(String batchIds) {
        this.batchIds = batchIds;
    }
}
